package ${model.packageName}.${model.modulePackage}.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import com.apa.biz.common.CustomRowBounds; 
import com.apa.biz.enums.SoftVersionType;
import com.apa.biz.exception.ServiceException; 
import com.apa.biz.utils.EncryptUtil;
import com.apa.biz.utils.UUIDUtil;
import ${model.packageName}.${model.modulePackage}.entity.${model.model}Entity; 
import ${model.packageName}.${model.modulePackage}.dao.${model.model}Dao; 
/**
* ${model.model}Service: ${model.comment} 业务服务层接口
*@author ${model.author} ${date}
*/
@Service
public class ${model.model}Service {

	@Autowired
    private ${model.model}Dao ${model.modelL}Dao;
	 
	/**
	 * 通过code查询实体
	 * @param code
	 * @return
	 */
	public ${model.model}Entity findByCode(String code){
		return ${model.modelL}Dao.findByCode(code);
	}
	
	 
	
	
	/**       
	*  逻辑删除
	* @param code
	* @return      
	*/
	@Transactional
	public boolean delete${model.model}ByCode(String code){
		try {
			if(${model.modelL}Dao.deleteByCode(code)>0)
				return true;
			else
				return false;
		} catch (Exception e) {
			return false;
		}
		
	}
	
	
	/**       
	* 分页查询列表  
	* @param params
	* @param rowBounds
	* @return           
	*/
	public Page<Map<String, Object>> selectListPage(
			Map<String, Object> params, CustomRowBounds rowBounds) {
		try {
			List<Map<String, Object>> contentList = ${model.modelL}Dao.selectListPage(params, rowBounds);
			return new PageImpl<Map<String, Object>>(contentList, rowBounds, ${model.modelL}Dao.selectCount(params));
		} catch (Exception e) {
			throw new ServiceException(String.format("根据分页对象查询列表出错！语句:%s", "listPageInfo"), e);
		}
	}
	/**       
	* 分页查询合计
	* @param params
	* @param rowBounds
	* @return      
	*/
	public List<Map<String, Object>> selectSum(Map<String, Object> params) {
		try {
			return ${model.modelL}Dao.selectSum(params);
		} catch (Exception e) {
			throw new ServiceException(String.format("根据分页对象查询列表出错！语句:%s", "listSum"), e);
		}
	}
	
	/**       
	* 新增 
	* @param params
	* @param rowBounds
	* @return      
	*/
	@Transactional
	public boolean save${model.model}Add(Map<String, Object> params,${model.model}Entity ${model.modelL}Entity) {
		boolean result = false;
		try {
			
			//插入表记录
			String ${model.modelL}Code = UUIDUtil.getUUID32();
			${model.modelL}Entity.setCode(${model.modelL}Code);
			 ${model.modelL}Entity.setCreateCode((String)params.get("userCode"));
			${model.modelL}Entity.setCreateTime(new Date());
			${model.modelL}Entity.setDelFlag(0);
			${model.modelL}Dao.save(${model.modelL}Entity);
			 
			 
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
			throw new ServiceException("新增保存异常", e);//自定义异常
		}
	}
	
	/**       
	* 修改
	* @param params
	* @param rowBounds
	* @return 
	*/
	@Transactional
	public boolean save${model.model}Edit(${model.model}Entity ${model.modelL}Entity) {
		boolean result = false;
		try {
			//修改表记录
			${model.model}Entity ${model.modelL} = ${model.modelL}Dao.findByCode(${model.modelL}Entity.getCode());
			  
#foreach($field in $fieldList)
#if(${field.isPk}!='Y'&&${field.field}!="code"&&${field.field}!="createCode"&&${field.field}!="updateCode"&&${field.field}!="updateTime"&&${field.field}!="createTime"&&${field.field}!="delFlag")
			${model.modelL}.set${field.fieldUp}(${model.modelL}Entity.get${field.fieldUp}());
#end
#end

			${model.modelL}.setUpdateCode(${model.modelL}Entity.getUpdateCode());
			${model.modelL}.setUpdateTime(new Date());
			${model.modelL}Dao.save(${model.modelL});
			 
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//事物回滚
			throw new ServiceException("修改异常", e);//自定义异常
		}
	}
	
	/**
	 * 新增或修改实体
	 * */
	public ${model.model}Entity save(${model.model}Entity entity){
		return ${model.modelL}Dao.save(entity);
	}
	
	 
}